using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Qreed.Windows.Forms;
using System.IO;
using Qreed.IO;
using Qreed.CodePlex;
using Qreed.Windows;

namespace FolderTreeView.Demo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            folderTreeView1.PopulateTree();

            RefreshTest();
                        
            string[] folders = { "c:\\windows\\system32\\drivers",
                                 "c:\\windows\\system32\\drivers",
                                 "c:\\windows\\system32\\drivers\\etc"};

            string commonBase = IOHelper.GetCommonBaseFolder(folders);

            //versionChecker.CheckForNewVersionASync();

            folderTreeView1.SelectedPath = @"C:\Users\null\Desktop";

            SystemImageList.AssignToListView(listView1);
        }

        private void RefreshTest()
        {
            /*if (Directory.Exists(@"i:\temp\test"))
            {
                Directory.Delete(@"i:\temp\test");
            }

            if (Directory.Exists(@"i:\temp\test2"))
            {
                Directory.Delete(@"i:\temp\test2");
            }

            folderTreeView1.SelectedPath = @"i:\temp";

            Directory.CreateDirectory(@"i:\temp\test");

            folderTreeView1.SelectedPath = @"i:\temp\test";

            Directory.CreateDirectory(@"i:\temp\test2");

            folderTreeView1.SelectedPath = @"i:\temp\test2";
            */
        }

        private void folderTreeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            textBox1.Text = folderTreeView1.SelectedPath;

            UpdateListView(folderTreeView1.SelectedPath);
        }

        private void UpdateListView(string path)
        {
            listView1.Items.Clear();

            try
            {
                if (folderTreeView1.SelectedShellFolder != null)
                {
                    ShellPIDL[] items = folderTreeView1.SelectedShellFolder.GetChildren(ShellFolder.ItemType.Folder|ShellFolder.ItemType.File|ShellFolder.ItemType.Hidden);

                    foreach (ShellPIDL item in items)
                    {
                        ListViewItem lvi = new ListViewItem();
                        lvi.Text = item.DisplayName;
                        lvi.ImageIndex = item.IconIndex;

                        listView1.Items.Add(lvi);
                    }
                }
            }
            catch 
            { 

            }

            
        }

        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                folderTreeView1.SelectedPath = textBox1.Text;
            }
        }

        private void refreshToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (folderTreeView1.SelectedNode != null)
            {
                ((ShellTreeNode)folderTreeView1.SelectedNode).Refresh();
            }
        }

        private void versionChecker_CheckForNewVersionComplete(object sender, VersionCheckerEventArgs e)
        {
            if (e.Error != null)
                MessageBox.Show(e.Error.ToString(), "Error while checking for a new version.");
            else
                MessageBox.Show(e.NewVersionAvailable.ToString());
        }
    }
}